<template>
  <up-upload
      :file-list="fileList"
      @afterRead="handleAfterRead"
      @delete="handleDelete"
      :maxCount="maxCount"
	  accept="image/*,video/*"
	  maxSize="10000000"
	  :multiple="false"
	  capture=""
  >
  </up-upload>
</template>

<script setup>
import { ref } from 'vue';
// import { baseFileUrl, basePreview } from "@/config/baseUrl";
// import { postUserComment } from "@/config/api";
// import { postUserComplaint } from "@/config/api";
// import { getUserComment } from "@/config/api";
// import { getUserComplaint } from "@/config/api";

const props = defineProps({
  multiple: {
    type: Boolean,
    default: true
  },
  maxCount: {
    type: Number,
    default: 9
  }
});

const emits = defineEmits(['update:fileList']);

const fileList = ref([])
const handleDelete = (event) => {
  fileList.value.splice(event.index, 1);
  emits('update:fileList', fileList.value);
};
// function beforeRead(file) {
//       if (file.size > 10 * 1024 * 1024) {
//         uni.showToast({ title: '文件不能超过10MB', icon: 'none' })
//         return false
//       }
//       return true
//     }
const handleAfterRead = async (event) => {
  let lists = [].concat(event.file);
  let fileListLen = fileList.value.length;


  fileList.value.push(
    ...[].concat(event.file).map(item => ({
      ...item,
      status: 'uploading',
      message: '上传中'
    }))
  )
  emits('update:fileList', fileList.value);
  


  for (let i = 0; i < lists.length; i++) {
    const result = await uploadFilePromise(lists[i].url);
    let item = fileList.value[fileListLen];
    fileList.value.splice(fileListLen, 1, {
      ...item,
      status: 'success',
      message: '',
      url: basePreview + result,
    });
    fileListLen++;
  }
  emits('update:fileList', fileList.value);
};
const token = uni.getStorageSync("token");
const uploadFilePromise = (url) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: baseFileUrl + 'sys/common/upload',
      filePath: url,
      name: 'file',
      formData: {
        type: '05',
      },
	  header:{
	    'x-access-token':token || ''
	  },
      success: (res) => {
        setTimeout(() => {
          let rs = JSON.parse(res.data);
          resolve(rs.message);
        }, 1000);
      },
      fail: (err) => {
        console.error('上传失败:', err);
        reject(err);
      }
    });
  });
};

</script>
<style lang="scss" scoped>
::v-deep video{
	object-fit: cover !important;
	width: 100% !important;
	object-position: center !important;
}
</style>
